//
//  AFNTestViewController.m
//  HTTPTest
//
//  Created by youbing on 2017/2/7.
//  Copyright © 2017年 youbing. All rights reserved.
//

#import "AFNTestViewController.h"
#import <AFNetworking.h>

@interface AFNTestViewController ()
@property (nonatomic, strong) AFHTTPSessionManager *manager;/**<*/
@end

@implementation AFNTestViewController
- (void)viewDidLoad {
    [super viewDidLoad];
}
#pragma mark - btnClick
- (IBAction)getBegin {
  NSURLSessionDataTask *task =  [self.manager GET:@"http://ip.taobao.com/service/getIpInfo.php?ip=$ip" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@",responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error.localizedDescription);
    }];
}
- (IBAction)post {
    [self.manager POST:@"" parameters:@{} progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

- (IBAction)download {
   NSURLSessionDownloadTask *downTask = [self.manager downloadTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://img-download.pchome.net/download/1k1/2w/41/of2txd-1a6l.jpg"]] progress:^(NSProgress * _Nonnull downloadProgress) {
        NSLog(@"%lld--%lld",downloadProgress.totalUnitCount ,downloadProgress.completedUnitCount);
    } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
        NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        //拼接文件全路径
        NSString *fullpath = [caches stringByAppendingPathComponent:response.suggestedFilename];
        NSURL *filePathUrl = [NSURL fileURLWithPath:fullpath];
        return filePathUrl;
    } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
        NSLog(@"%@",filePath.path);
    }];
    
    [downTask resume];
}
#pragma mark -
- (AFHTTPSessionManager *)manager{
    if (!_manager) {
        _manager  = [AFHTTPSessionManager manager];
        _manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    }
    return _manager;
}
@end
